<?php

if(preg_match("/vid=([^\&]+)/",$this->url,$nn)){
    $vid=$nn[1];
}


if(preg_match("/mp\/homepage\?__biz=/",$this->url,$nn)){
    $this->url=$this->url."&begin=1&count=10&action=appmsg_list&f=json&r=0.076812862".time()."&appmsg_token=";
    $nr=@file_get_contents($this->url);
    $html=json_decode($nr,1)['data']['html'];
    preg_match_all("/<a\s+class=\"list_item\s+js_post\"\s+href=\"([^<>]+)\">.*<h2\s+class=\"title\s+js_title\">(.*)<\/h2>/Uisx",$html,$nn22);
}

if(preg_match_all("/id=\"li_([^\&\"]+)\"/U",$this->yuannr,$nn)){
    $vid=$nn[1][0];
}

if($vid!="") {
    $xml = @file_get_contents("http://vv.video.qq.com/geturl?vid={$vid}&otype=xml&platform=1&ran=0%2E9652906153351068");
    preg_match_all("/<url>(.*)<\/url>/Uisx", $xml, $nn);
    for ($i = 0; $i < count($nn[1]); $i++) {
        //$nn[1][$i] = preg_replace("/&/", "&", $nn[1][$i]);
        $this->swfstr .= "[flash]" . $nn[1][$i] . "[/flash]\r\n" . "\r\n";
    }
}

if(!class_exists('getInfo',false)) {
    class getInfo
    {
        //获取视频播放地址
        public static function getVideoInfo($url)
        {
//        $url = "https://v.qq.com/x/page/c0025lmctmo.html";
            preg_match("/\/([0-9a-zA-Z]+).html/", $url, $match);
            if (empty($match)) {
                return [
                    'code' => 0,
                    'msg' => 'success',
                    'url' => $url
                ];
            }
            $vid = $match[1];//视频ID
            try {
                set_time_limit(0);
                $getinfo = "http://vv.video.qq.com/getinfo?vids={$vid}&platform=11&charge=0&otype=xml";
                $info = self::normal_curl($getinfo);
                $info_arr = self::xmlToArray($info);
                if ($info_arr['msg'] == 'vid is wrong') {
                    return [
                        'code' => 1,
                        'msg' => '视频出错',
                        'url' => $url
                    ];
                }
                $fi = $info_arr['fl']['fi'];
                if (isset($fi[1])) {
                    $format_id = $fi[1]['id'];
                    $fmt = $fi[1]['name'];
                    $format = 'p' . substr($format_id, -3, 3);
                    $key = $info_arr['vl']['vi']['fvkey'];
                    $vid = $info_arr['vl']['vi']['vid'];
                    $url = $info_arr['vl']['vi']['ul']['ui'][0]['url'];
                    if (strlen($format_id) >= 5) {
                        $mp4 = $vid . '.' . $format . '.1.mp4';
                    } else {
                        $mp4 = $vid . '.mp4';
                    }
                    $video_url = $url . $mp4 . '?vkey=' . $key . '&fmt=' . $fmt;

                } else {
                    $getinfo = "http://vv.video.qq.com/getinfo?vids={$vid}&platform=101001&charge=0&otype=xml";
                    $info = self::normal_curl($getinfo);
                    $info_arr = self::xmlToArray($info);
                    if (isset($info_arr['msg']) && $info_arr['msg'] == 'vid is wrong') {
                        return [
                            'code' => 0,
                            'msg' => '视频出错',
                            'url' => $url
                        ];
                    }
                    $filename = $info_arr['vl']['vi']['fn'];
                    $key = $info_arr['vl']['vi']['fvkey'];
                    $url = $info_arr['vl']['vi']['ul']['ui'][0]['url'];
                    $video_url = $url . $filename . '?vkey=' . $key;
                }
                return [
                    'code' => 0,
                    'msg' => 'success',
                    'url' => $video_url
                ];

            } catch (\Exception $e) {
                return [
                    'code' => 0,
                    'msg' => 'success',
                    'url' => $url
                ];
            }
        }

        //http网址访问
        public function normal_curl($url)
        {
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_HEADER, 0);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            $data = curl_exec($curl);
            //错误提示
            if (curl_exec($curl) === false) {
                die(curl_error($curl));
            }
            // 检查是否有错误发生
            if (curl_errno($curl)) {
                echo 'Curl error: ' . curl_error($curl);
            }
            curl_close($curl);
            return $data;
        }

        //https网址访问
        public function getHTTPS($url)
        {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_HEADER, false);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_REFERER, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
            $result = curl_exec($ch);
            curl_close($ch);
            return $result;
        }

        //将XML转为array
        public function xmlToArray($xml)
        {
            //禁止引用外部xml实体
            libxml_disable_entity_loader(true);
            $values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
            return $values;
        }
    }
}

if(preg_match("/v\.qq\.com/",$this->url)){
	$qqtv=new getInfo();
	$url=$this->url;
	$result=$qqtv->getVideoInfo($url);
	$this->flash($result[url]);
}

//if(preg_match("/nickname\s*=\s*\"(.*)\"/Uisx",$tempnr,$nn)){
//    $addreturn['zmt']['zmt']="微信公众号";
//    $addreturn['zmt']['zmthao']=$nn[1];
//    $addreturn['zmt']['zwmc']=$nn[1];
//    preg_match("/var\s+ct\s+=\s+\"([^<>]*)\";/Uisx",$tempnr,$nn);
//    $addreturn['zmt']['ct']=$nn[1];
//    preg_match("/<label\s+class=\"profile_meta_label\">微信号<\/label>\s+<span\s+class=\"profile_meta_value\">([^<>]*)<\/span>/Uisx",$tempnr,$nn);
//    $addreturn['zmt']['zmthaoyingwen']=$nn[1];
//    $addreturn['zmt']['homeurl']="https://new.qq.com/omn/author/{$addreturn['zmt']['zmthaoyingwen']}";
//    $home= element($addreturn['zmt']['homeurl'], ".portrait", "", "", 15);
//    $addreturn['zmt']['avatar']="https:".phpQuery($home,".portrait:first")->attr("src");
//    $addreturn['zmt']['datasourceurl']=$addreturn['zmt']['homeurl']."?zq=1";
//    $addreturn['zmt']['addreturn']=json_encode($addreturn);
//}
//
//if(preg_match("/\"media\"\:\s+\"([^<>]*)\"\,/Uisx",$tempnr,$nn)){
//    $addreturn['zmt']['zmt']="企鹅号";
//    $addreturn['zmt']['zmthao']=$nn[1];
//    $addreturn['zmt']['zwmc']=$nn[1];
//    preg_match("/\"media_id\"\:\s+\"([^<>]*)\"\,/Uisx",$tempnr,$nn);
//    $addreturn['zmt']['zmthaoyingwen']=$nn[1];
//    $addreturn['zmt']['homeurl']="https://new.qq.com/omn/author/{$addreturn['zmt']['zmthaoyingwen']}";
//    $home= element($addreturn['zmt']['homeurl'], ".portrait", "", "", 15);
//    $addreturn['zmt']['avatar']="https:".phpQuery($home,".portrait:first")->attr("src");
//    $addreturn['zmt']['datasourceurl']=$addreturn['zmt']['homeurl']."?zq=1";
//    $addreturn['zmt']['addreturn']=json_encode($addreturn);
//}
//
//if(preg_match("/\/m\/author\?id=([0-9]+)/",$this->url,$nn3)&&preg_match("/style=\"background\-image\:\s + url\("([^<>]*)"\);\"><\/div>/Uisx",$tempnr,$nn)){
//    preg_match("/head\"><span\s+class=\"jsx\-[^<>]*\s+name\">([^<>]*)<\/span>/Uisx",$tempnr,$nn2);
//    $addreturn['zmt']['zmt']="企鹅号";
//    $addreturn['zmt']['zmthao']=$nn2[1];
//    $addreturn['zmt']['zwmc']=$nn2[1];
//    $addreturn['zmt']['zmthaoyingwen']=$nn3[1];
//    $addreturn['zmt']['homeurl']="https://xw.qq.com/m/author?id={$nn3[1]}";
//    $addreturn['zmt']['avatar']="https:".$nn[1];
//    preg_match("/<div\s+class=\"jsx\-[^<>]*\s+desc\">([^<>]*)<\/div>/Uisx",$tempnr,$nn4);
//    $addreturn['zmt']['summary']=$nn4[1];
//    $addreturn['zmt']['datasourceurl']=$addreturn['zmt']['homeurl'];
//    $addreturn['zmt']['addreturn']=json_encode($addreturn);
//}




?>